package team.fourth.combat.commen.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import team.fourth.combat.commen.entity.SmsResponse;

public class SmsUtil {


    public static SmsResponse sendSmsCode(String phoneNumbers, String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI4FoWPcDwEDm996e22irM",//1、替换成你的
                "Gchgagrd3mG33OAGaitbZpbz2JSxcD");//2、替换成你的
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", "传智健康");//3、替换成你的
        request.putQueryParameter("TemplateCode", "SMS_183145313");////4、替换成你的

        JSONObject param = new JSONObject();
        param.put("code",code);

        request.putQueryParameter("TemplateParam",  param.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            SmsResponse smsResponse = JSONObject.parseObject(response.getData(), SmsResponse.class);
            return smsResponse;
            //{"Message":"OK","RequestId":"FF9920D3-BD00-4FFE-9166-26D37B6B319F","BizId":"911711179229572095^0","Code":"OK"}
        } catch (ServerException e) {
            e.printStackTrace();
            //处理超时
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        sendSmsCode("15601795090","12313");
    }

}
